package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.datereport;

import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.RepTemplateMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.RepTemplate;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.datareport.JsmRepTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by huangxueqian on 2017/2/14/014.
 * 报表模板服务实现类
 */

public class JsmRepTemplateServiceImpl implements JsmRepTemplateService {



    @Autowired
    private RepTemplateMapper repTemplateMapper ;

    /**
     *
     * @param  repTemplate
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject table(RepTemplate repTemplate) throws Exception {
        JSONObject jsonObject = new JSONObject();
        /**
         *
         */
        jsonObject.put("total",repTemplateMapper.queryTableCount(repTemplate));
        jsonObject.put("rows",repTemplateMapper.queryTableList(repTemplate));
        /**
         *
         */
        return jsonObject;
    }

    /**
     *
     * @param repTemplate
     * @return 更新或者删除问题
     * @throws Exception
     */
    @Override
    public Integer add(RepTemplate repTemplate,Employee employee) throws Exception {
        Integer resInt = 0 ;
        if(repTemplate.getReportId()==null){
            repTemplate.setReportId(UniqueUtil.uuid());
            repTemplate.setCreatetime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            repTemplate.setModifytime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            repTemplate.setCreator(employee.getEmployeeId());
            repTemplate.setModifyer(employee.getEmployeeId());
            resInt = repTemplateMapper.insertSelective(repTemplate);
        }else{
            repTemplate.setModifytime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM_SS));
            repTemplate.setModifyer(employee.getEmployeeId());
            resInt = repTemplateMapper.updateByPrimaryKeySelective(repTemplate);
        }
        return resInt;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer del(List<String> reportIdList) throws Exception {
        Integer resInt = 0 ;
        if(reportIdList!=null &&reportIdList.size()>0){
            for(String reportId : reportIdList){
                repTemplateMapper.deleteByPrimaryKey(reportId);
            }
        }
        return resInt;
    }

    @Override
    public RepTemplate queryRemplatePrimaryKey(String repTemplate) throws Exception {
        return null;
    }

    @Override
    public RepTemplate queryRemplate(RepTemplate repTemplate) throws Exception {
        return null;
    }
}
